package cn.edu.hqu.javaee.Controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.hqu.javaee.entity.Load;
import cn.edu.hqu.javaee.entity.Student;
import cn.edu.hqu.javaee.entity.curriculum;
import cn.edu.hqu.javaee.respority.CourseRespority;
import cn.edu.hqu.javaee.respority.LoadRespority;
import cn.edu.hqu.javaee.respority.StudentRespority;

@Controller
@RequestMapping("/home")
public class HomeController {
	
	String user=null;
	
	@Autowired
	private StudentRespority studentRespority;
	
	@Autowired
	private LoadRespority loadRespority;
	
	@Autowired
	
	private CourseRespority courseRespority;
	
	//主页面
	@GetMapping("/Home")
	public String home(Load v,Model model,HttpServletRequest request) {
		
		String username=request.getParameter("myname");
		Load load=loadRespority.findById(username==null?"1825122025":username).get();
		System.out.print(load);
		user=username;
//		if(load!=null) {
//			model.addAttribute("load", load);			
//		}
		return "Home";
	}
	//注册页面
	@GetMapping("/Register")
	public String regisiter(Student s,HttpServletRequest request) {
		String userId=request.getParameter("myId");
		String userPassword=request.getParameter("myPassword");
		String userName=request.getParameter("myName");
		String userCollege=request.getParameter("myCollege");
		String userMajor=request.getParameter("myMajor");
		String userAge=request.getParameter("myAge");
		Student student=new Student();
		Load newLoad=new Load();
		
		if(userId!=""&&userPassword!=""&&userName!=""&&userCollege!=""&&userMajor!=""&&userAge!="") {
			student.setId(userId==null?"1":userId);
			student.setName(userName);
			student.setCollege(userCollege);
			student.setMajor(userMajor);
			student.setAge(userAge);
			
			newLoad.setId(userId==null?"1":userId);
			newLoad.setPassword(userPassword);
			
			studentRespority.save(student);
			loadRespority.save(newLoad);			
		}
				
		student.setId(null);
		student.setName(null);
		student.setCollege(null);
		student.setMajor(null);
		student.setAge(null);
		
		return "Register";
	}
	//选课页面
	@GetMapping("/Course")
	public String Course(curriculum v,HttpServletRequest request) {
		String []list = request.getParameterValues("checkbox");
		 if(list==null){
		        System.out.println("none list");
			 }else{	        
		        for (int i=0;i<list.length;i++) {
		        	curriculum course=new curriculum();
		        	course.setId(user+(i+1));
		        	course.setCourse(list[i]);
		        	courseRespority.save(course);
		          }
			 }
		 return "Course";
	}
	//个人信息页面
	@GetMapping("/PersonalCenter")
	public String PersonalCenter(Student v,Model model) {
		Student	student1=studentRespority.findById(user==null?"1825122025":user).get();
		model.addAttribute("student", student1);	
		return "PersonalCenter";
	}
	//展示页面
	@GetMapping("/End")
	public String end(curriculum v,Model model) {
			
		    Student	student1=studentRespority.findById(user==null?"1825122025":user).get();
		    model.addAttribute("student", student1);
		
			curriculum course1=courseRespority.findById(user==null?"18251220251":user+1).get();
			model.addAttribute("course1", course1);
			if(course1==null) {
				return "End";			
		    }
			
			curriculum course2=courseRespority.findById(user==null?"18251220252":user+2).get();
			model.addAttribute("course2", course2);
			if(course2==null) {
				return "End";			
		    }
			curriculum course3=courseRespority.findById(user==null?"18251220253":user+3).get();
			model.addAttribute("course3", course3);
			if(course3==null) {
				return "End";			
		    }
			curriculum course4=courseRespority.findById(user==null?"18251220254":user+4).get();
			model.addAttribute("course4", course4);
			if(course4==null) {
				return "End";			
		    }
			curriculum course5=courseRespority.findById(user==null?"18251220255":user+5).get();
			model.addAttribute("course5", course5);
			if(course5==null) {
				return "End";			
		    }
		return "End";
	}
	
}